//
//  ZYCMainViewController.m
//  ZDSC
//
//  Created by ZDTX on 2021/5/14.
//

#import "ZYCMainViewController.h"
#import "ZYCNavigationController.h"
#import "ZYCMessageRootViewController.h"
#import "ZYCScheduleViewController.h"
#import "ZYCHomeRootViewController.h"
#import "ZYCApplianceRootViewController.h"
#import "ZYCProfileRootViewController.h"
#import "ZYCHomeViewController.h"

@interface ZYCMainViewController ()

@end

@implementation ZYCMainViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    //去掉tabBar顶部线条
    CGRect rect = CGRectMake(0, 0, self.tabBar.frame.size.width, self.tabBar.frame.size.height);
    
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [[UIColor clearColor] CGColor]);
    CGContextFillRect(context, rect);
    UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    [self.tabBar setBackgroundImage:img];
    [self.tabBar setShadowImage:img];
    
    self.tabBar.translucent = NO;
    
    self.tabBar.tintColor = [UIColor colorWithHexString:@"#009B4C" alpha:1.0];


    
    [[UITabBarItem appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName :HEX_RGB(kTitleMainColor) } forState:UIControlStateSelected];
    
    [[UITabBarItem appearance] setTitleTextAttributes:@{NSFontAttributeName:kCustomFont(@"PingFangSC-Regular", 12.0f)} forState:UIControlStateNormal];
    [[UITabBarItem appearance] setTitleTextAttributes:@{NSFontAttributeName:kCustomFont(@"PingFangSC-Regular", 12.0f)} forState:UIControlStateSelected];

    
    [self setUpChildVC:[ZYCMessageRootViewController new] title:NSLocalizedString(@"消息", nil) image:@"xiaoxi" selectedImage:@"xiaoxi_s"];
    [self setUpChildVC:[ZYCScheduleViewController new] title:NSLocalizedString(@"日程", nil) image:@"peixun" selectedImage:@"richen_s"];
    [self setUpChildVC:[ZYCHomeViewController new] title:@"" image:@"shouye" selectedImage:@"shouye"];
    [self setUpChildVC:[ZYCApplianceRootViewController new] title:NSLocalizedString(@"应用", nil) image:@"juzheng" selectedImage:@"yingyong_s"];
    [self setUpChildVC:[ZYCProfileRootViewController new] title:NSLocalizedString(@"我的", nil) image:@"wode" selectedImage:@"wode_s"];
    // 默认选择首页
    self.selectedIndex = 2;
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateBadgeNum:) name:ZYCUnRedMessageCountNotification object:nil];
}

- (void)updateBadgeNum:(NSNotification *)noti{
    UITabBarItem *messageItem = self.tabBar.items.firstObject;
    NSInteger totalCount = [noti.userInfo[@"totalCount"] integerValue];
    NSString *str = nil;
    if (totalCount > 100) {
        str = @"99+";
    }else if(totalCount <= 0){
        str = nil;// 默认值
    }else{
        str = [NSString stringWithFormat:@"%ld",(long)totalCount];
    }
    messageItem.badgeValue = str;
}

- (void)setUpChildVC:(UIViewController *)vc title:(NSString *)title image:(NSString *)image selectedImage:(NSString *)selectedImage {
    vc.tabBarItem.title = title;
    vc.tabBarItem.image = [[UIImage imageNamed:image] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    vc.tabBarItem.selectedImage = [[UIImage imageNamed:selectedImage] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    [vc.tabBarItem setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:HEX_RGB(kTitleMainColor),NSForegroundColorAttributeName, kCustomFont(@"PingFangSC-Regular", 12.0f),NSFontAttributeName,nil] forState:UIControlStateSelected];
    //设置图片大小及位置
    if (title.length == 0) {
        vc.tabBarItem.imageInsets = UIEdgeInsetsMake(-20, 30, 20, 30);
    }else{
        vc.tabBarItem.imageInsets = UIEdgeInsetsMake(0, 10, -5, 10);
    }

    ZYCNavigationController *navc = [[ZYCNavigationController alloc] initWithRootViewController:vc];
    [self addChildViewController:navc];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
